package com.example.hetong_system.controller;

import com.example.hetong_system.model.Clause;
import com.example.hetong_system.service.ClauseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/auth")
public class ClauseController {

    @Autowired
    private ClauseService clauseService;

    // 创建新条款
    @PostMapping("/clauses/create")
    public ResponseEntity<Clause> createClause(@RequestBody String content) {
        Clause clause = clauseService.createClause(content);
        return ResponseEntity.ok(clause);
    }

    // 关联条款到合同
    @PostMapping("/clauses/associate")
    public ResponseEntity<Void> associateClauseWithContract(
            @RequestBody ClauseAssociationRequest request) {
        clauseService.associateClauseWithContract(
                request.getContractId(),
                request.getClauseId(),
                request.getType()
        );
        return ResponseEntity.noContent().build();
    }

    // 获取合同的所有条款
    @GetMapping("/clauses/contract/{contractId}")
    public ResponseEntity<?> getClausesByContractId(@PathVariable Long contractId) {
        return ResponseEntity.ok(clauseService.getClausesByContractId(contractId));
    }

    // 获取未关联到合同的条款（分页）
    @GetMapping("/clauses/available/{contractId}")
    public ResponseEntity<?> getUnassociatedClauses(
            @PathVariable Long contractId,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {

        Page<Clause> clauses = clauseService.getUnassociatedClauses(contractId, page, size);

        Map<String, Object> result = new HashMap<>();
        result.put("content", clauses.getContent());
        result.put("totalPages", clauses.getTotalPages());
        result.put("totalElements", clauses.getTotalElements());
        result.put("currentPage", clauses.getNumber());
        result.put("pageSize", clauses.getSize());

        return ResponseEntity.ok(result);
    }

    // 从合同中移除条款
    @DeleteMapping("/clauses/{clauseId}/contract/{contractId}")
    public ResponseEntity<Void> removeClauseFromContract(
            @PathVariable Long contractId,
            @PathVariable Long clauseId) {
        clauseService.removeClauseFromContract(contractId, clauseId);
        return ResponseEntity.noContent().build();
    }

    // 辅助类，用于接收关联请求
    static class ClauseAssociationRequest {
        private Long contractId;
        private Long clauseId;
        private String type = "INITIAL"; // 默认初始条款

        public Long getContractId() {
            return contractId;
        }

        public void setContractId(Long contractId) {
            this.contractId = contractId;
        }

        public Long getClauseId() {
            return clauseId;
        }

        public void setClauseId(Long clauseId) {
            this.clauseId = clauseId;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }
    }
}